import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:yp/Xwidgets/cache_image.dart';
import 'package:yp/config/config.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../../helper.dart';
import 'logic.dart';
import 'state.dart';

class MyCollectPage extends StatefulWidget {
  MyCollectPage({Key key}) : super(key: key);

  @override
  _MyCollectPageState createState() {
    return _MyCollectPageState();
  }
}

class _MyCollectPageState extends State<MyCollectPage> {
// class _MyCollectPageState extends GetView<MyCollectController> {
  final MyCollectState state = Get.find<MyCollectController>().state;
  final MyCollectController logic = Get.put(MyCollectController());
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xfff5f5f5),
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: Color(0xffEA5504),
        elevation: 0,
        title: Text('我的收藏'),
      ),
      body: GetBuilder<MyCollectController>(
        init: MyCollectController(),
        builder: (logic) {
          return Container(
            color: Color(0xfff5f5f5),
            child: Column(
              children: [
                Container(
                  height: 60,
                  padding: EdgeInsets.only(bottom: 12.0),
                  color: Color(0xffffffff),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: [
                      InkWell(
                        onTap: () {
                          logic.tabSelect(context, 1);
                        },
                        child: Container(
                          padding: EdgeInsets.only(right: 38),
                          width: MediaQuery.of(context).size.width / 2,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.end,
                            crossAxisAlignment: CrossAxisAlignment.end,
                            children: [
                              Text(
                                '商品',
                                style: TextStyle(
                                  color: !state.shoppingSelect
                                      ? Color(0xff333333)
                                      : Config.mainColor,
                                  fontSize: 16.0,
                                  fontWeight: FontWeight.bold,
                                ),
                                textAlign: TextAlign.right,
                              ),
                              Container(
                                margin: EdgeInsets.only(top: 4),
                                width: 28.0,
                                height: 4,
                                color: !state.shoppingSelect
                                    ? Color(0xffffffff)
                                    : Config.mainColor,
                              ),
                            ],
                          ),
                        ),
                      ),
                      InkWell(
                        onTap: () {
                          logic.tabSelect(context, 2);
                        },
                        child: Container(
                          padding: EdgeInsets.only(left: 38),
                          width: MediaQuery.of(context).size.width / 2,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.end,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                '店铺',
                                style: TextStyle(
                                  color: state.shoppingSelect
                                      ? Color(0xff333333)
                                      : Config.mainColor,
                                  fontSize: 16.0,
                                  fontWeight: FontWeight.bold,
                                ),
                                textAlign: TextAlign.left,
                              ),
                              Container(
                                margin: EdgeInsets.only(top: 4),
                                width: 29.0,
                                height: 4,
                                color: state.shoppingSelect
                                    ? Color(0xffffffff)
                                    : Config.mainColor,
                              ),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                state.shoppingSelect
                    ? state.collectLists.length > 0
                        ? Expanded(
                            child: Container(
                              color: Color(0xfff5f5f5),
                              child: ListView.builder(
                                shrinkWrap: true,
                                // physics: NeverScrollableScrollPhysics(),
                                itemCount: state.collectLists.length,
                                itemBuilder: (BuildContext context, int index) {
                                  return Container(
                                    color: Color(0xfff5f5f5),
                                    padding: EdgeInsets.only(
                                      top: setWidth(24.0),
                                    ),
                                    child: _lists(
                                        index, state.collectLists[index]),
                                  );
                                },
                              ),
                            ),
                          )
                        : _noMsg()
                    : state.shopCollectLists.length > 0
                        ? Expanded(
                            child: Container(
                              color: Color(0xfff5f5f5),
                              child: ListView.builder(
                                shrinkWrap: true,
                                // physics: NeverScrollableScrollPhysics(),
                                itemCount: state.shopCollectLists.length,
                                itemBuilder: (BuildContext context, int index) {
                                  return Container(
                                    color: Color(0xfff5f5f5),
                                    padding: EdgeInsets.only(
                                      top: setWidth(24.0),
                                    ),
                                    child: _shopLists(
                                        index, state.shopCollectLists[index]),
                                  );
                                },
                              ),
                            ),
                          )
                        : _noMsg(),
              ],
            ),
          );
        },
      ),
    );
  }

  // 店铺列表
  Widget _shopLists(int index, item) {
    return InkWell(
      onTap: () {
        logic.getAndGoThirdShop(item['ec_shop_num_id']);
      },
      child: Container(
        height: 260.w,
        margin: EdgeInsets.only(bottom: 20.w),
        padding: EdgeInsets.only(top: 50.w, left: 10.0, right: 10.w),
        color: Color(0xffffffff),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
                width: 160.w,
                height: 160.w,
                margin: EdgeInsets.only(right: 20.w),
                // padding: EdgeInsets.only(top: 60.w),
                color: Color(0xfff5f5f5),
                child: Image.network(
                    item['ec_shop_logo'] == null || item['ec_shop_logo'] == ''
                        ? Config.orderEmpty
                        : item['ec_shop_logo'])
                // Text(
                //   '暂无图片',
                //   textAlign: TextAlign.center,
                // ),
                ),
            Expanded(
              child: Container(
                child: Text(item['ec_shop_name']),
              ),
            ),
            InkWell(
              onTap: () {
                logic.collecShopDelete(
                  context,
                  item['ec_shop_num_id'],
                );
              },
              child: Image.asset(
                'assets/images/litterBasket_image.png',
                width: setWidth(42),
                height: setWidth(42),
                color: Color(0xffcccccc),
                fit: BoxFit.cover,
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _lists(int index, var collectLists) {
    return InkWell(
      onTap: () {
        logic.enterGoodsDetail(
            collectLists['style_num_id'],
            collectLists['ec_style_num_id'],
            collectLists['cat_id'],
            collectLists['default_item_num_id'],
            collectLists['shop_num_id'],
            collectLists['is_pre_sale'] ?? 0);
      },
      child: Container(
        padding: EdgeInsets.all(16.0),
        margin: EdgeInsets.only(bottom: setWidth(12)),
        width: MediaQuery.of(context).size.width,
        color: Color(0xffffffff),
        // decoration: BoxDecoration(
        //   border: Border.all(
        //     color: Color(0xfffff000),
        //   ),
        // ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              child: Row(
                children: [
                  CachedImage(
                      width: 80.w,
                      height: 100.w,
                      margin: EdgeInsets.fromLTRB(0, 10, 8, 52),
                      scale: BoxFit.cover,
                      path: '${collectLists['image_default_id']}'),
                  SizedBox(width: 14),
                  Container(
                    width: 470.w,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.start,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text('${collectLists['ec_shop_name']}'),
                              Text('${collectLists['goods_name']}'),
                              Text(
                                '${collectLists['sub_title']}',
                                style: TextStyle(
                                  color: Color(0xffcccccc),
                                ),
                              ),
                            ],
                          ),
                        ),
                        SizedBox(
                          height: 12.w,
                        ),
                        Text(
                          '¥${collectLists['goods_price']}',
                          style: TextStyle(
                            color: Config.mainColor,
                            fontSize: 30.w,
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            Column(
              children: [
                InkWell(
                  onTap: () {
                    logic.collecDelete(
                      context,
                      collectLists['style_num_id'],
                      collectLists['ec_shop_num_id'],
                    );
                  },
                  child: Image.asset(
                    'assets/images/litterBasket_image.png',
                    width: setWidth(42),
                    height: setWidth(42),
                    color: Color(0xffcccccc),
                    fit: BoxFit.cover,
                  ),
                ),
                SizedBox(
                  height: 150.w,
                ),
                collectLists['fresh_type']==8?Container():  (collectLists['style_store'] > 0
    ? InkWell(
                        onTap: () {
                          logic.collectAddShoopingCart(
                              context,
                              collectLists['ec_shop_num_id'],
                              collectLists['style_num_id'],
                              collectLists['ec_style_num_id']);
                        },
                        child: Image.asset(
                          'assets/images/shoppingCart_image.png',
                          width: setWidth(42),
                          height: setWidth(42),
                          color: Config.mainColor,
                          fit: BoxFit.cover,
                        ),
                      )
                    : Text('已售罄')),
              ],
            )
          ],
        ),
      ),
    );
  }

  Widget _noMsg() {
    return Container(
      margin: EdgeInsets.only(top: 20.w),
      color: Color(0xffffffff),
      child: Center(
        child: Column(
          children: [
            SizedBox(
              height: 140.w,
            ),
            Image(
              image: AssetImage("assets/images/nodata.png"),
              width: 218.w,
              height: 164.w,
            ),
            SizedBox(
              height: 20.w,
            ),
            Text(
              '暂无数据',
              style: TextStyle(
                color: Color(0xff999999),
                fontSize: 28.w,
              ),
            ),
            SizedBox(
              height: 40.w,
            ),
          ],
        ),
      ),
    );
  }
}
